<?php
$this->headTitle('Phân quyền','append');
?>
<div class="grid">
    <div class="breadcrumbs">
        <a class="item-1" href="/index">Trang chủ</a> › 
        <a class="item-2" href="/admin/role"><?php $title = $this->headTitle(); if(isset($title[0])) echo $title[0]; ?></a> › 
        <span>Sửa</span>
        <div class="clear-both"></div>
    </div>
    <div class="box header">Sửa quyền mới</div>
    <div class="box content">
        <?php
            if(isset($this->mess)){
                if($this->flag == 'success')
                    echo '<div class="notification success">',$this->mess,'</div>';
                elseif($this->flag == 'alert')
                    echo '<div class="notification alert">',$this->mess,'</div>';
                else
                    echo '<div class="notification error">',$this->mess,'</div>';
            }
        ?>
        <form id="form" class="form-option" method="post" action="" enctype="multipart/form-data" name="form">
            <dl class="zend_form">
                <dt><label>Tên quyền</label></dt>
                <dd><input type="text" id="roleName" name="roleName" value="<?php echo $this->roleCheck['roleName'];?>"/></dd>
                <div class="clear-both"></div>
    
       
                
                <dt><label>Đối Tác</label></dt>
                <dd>
                    <div class="option-wrapper">
                        <div class="fieldset">
                            <div class="legend"><input type="checkbox" id="check_all_partnerId"/><label>Chọn hết</label></div>
                            <div class="fieldset-content">
                                <ul>
                                <?php 
                                    $partnerList = $this->roleCheck['partnerId'];
                                    $partnerList = json_decode($partnerList);
                                    foreach($this->partnerList as $partner){
                                        if(is_array($partnerList) && in_array($partner['partnerId'],$partnerList))
                                            echo "<li><input type='checkbox' typerole='partner' class='partnerId' name='partnerId[]' value='{$partner['partnerId']}' checked='checked'><label>{$partner['partnerName']}</label></li>";
                                        else
                                            echo "<li><input type='checkbox' typerole='partner' class='partnerId' name='partnerId[]' value='{$partner['partnerId']}'><label>{$partner['partnerName']}</label></li>";
                                    }
                                ?>
                                </ul>
                                <div class="clear-both"></div>
                            </div>
                        </div>
                    </div>
                </dd>
                <div id="game">
                <?php
                    $partnerId=$this->roleCheck['partnerId'];
                    $gameId=$this->roleCheck['gameId'];
                    $gameId = json_decode($gameId, true);
                    $partnerId = json_decode($partnerId);
                    foreach($partnerId as $row)
                    {?>
                        <div id="game<?php echo $row; ?>">
                        <dd>
                            <div class="option-wrapper_partner" style="margin-left:120px;">
                            <div class="fieldset" >
                            <div class="legend_partner">
                                <label >Game của <?php echo $row; ?></label>
                            </div>
                            <div class="fieldset-content">
                            <ul id="game_<?php echo $row; ?>">
                            <?php
                                $username = 'admin';
                                $password = '123456';
                                $loginUrl = 'http://local.admin.partner.vn/auth';
                                $ch = curl_init();
                                curl_setopt($ch, CURLOPT_URL, $loginUrl);
                                curl_setopt($ch, CURLOPT_POST, 1);
                                curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password);
                                curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                $store = curl_exec($ch);
                                curl_setopt($ch, CURLOPT_URL, "http://local.admin.partner.vn/admin/game/getgame/partnerId/{$row}");
                                //execute the request
                                $content = curl_exec($ch);
                                //save the data to disk
                                curl_close ($ch);
                                $content=json_decode($content, True);
                                foreach($content as $key=>$value){
                                    foreach($gameId as $k=>$v){
                                    if($k==$row){
                                        if(is_array($v) && in_array($value['gameId'],$v)){  ?>
                                            <li><input type="checkbox" class="gameId" name="gameId_<?php echo $row;?>[]" value="<?php echo $value['gameId'];?>" checked="checked"/> <label id="gameId"><?php echo $value['gameName'];?></label></li>
                                  <?php }else {?>
                                       <li><input type="checkbox" class="gameId" name="gameId_<?php echo $row;?>[]" value="<?php echo $value['gameId'];?>"/> <label id="gameId"><?php echo $value['gameName'];?></label></li> 
                                  <?php } 
                                    } 
                                    }
                                }//end foreach
                            ?>    
                            </ul>
                            <div class="clear-both">
                            </div></div></div> </div>
                        </dd>
                        </div>
                   <?php }
                ?>
                </div>
                <div class="clear-both"></div>
                <dt><label>Chức năng admin Tool</label></dt>
                <dd>
                    <div class="option-wrapper">
                        <div class="fieldset">
                            <div class="legend"><input type="checkbox" id="check_all_resource_admin_tool"/><label>Chọn hết</label></div>
                            <div class="fieldset-content">
                            <ul>
                            <?php 
                           
                                $resourceList = $this->roleCheck['resourceId'];
                                $resourceList = json_decode($resourceList);
                                foreach($this->resourceList as $resource){
                                    if($resource['moduleName'] == 'default'){
                                        if(isset($resourceList) && is_array($resourceList) && in_array($resource['resourceId'],$resourceList))
                                            echo "<li><input type='checkbox' class='resourceIdAdminTool' name='resourceId[]' value='{$resource['resourceId']}' checked='checked'><label>{$resource['resourceName']}</label></li>";
                                        else
                                            echo "<li><input type='checkbox' class='resourceIdAdminTool' name='resourceId[]' value='{$resource['resourceId']}'><label>{$resource['resourceName']}</label></li>";
                                    }
                                }
                            ?>
                            </ul>
                                <div class="clear-both"></div>
                            </div>
                        </div>
                    </div>
                </dd>
                <div class="clear-both"></div>
                <dt><label>Chức năng Admin KPI</label></dt>
                <dd>
                    <div class="option-wrapper">
                        <div class="fieldset">
                            <div class="legend"><input type="checkbox" id="check_all_resource_admin"/><label>Chọn hết</label></div>
                            <div class="fieldset-content">
                            <ul>
                            <?php 
                                $resourceList = $this->roleCheck['resourceId'];
                                $resourceList = json_decode($resourceList);
                                foreach($this->resourceList as $resource){
                                    if($resource['moduleName'] == 'admin'){
                                        if(isset($resourceList) && is_array($resourceList) && in_array($resource['resourceId'],$resourceList))
                                            echo "<li><input type='checkbox' class='resourceIdAdmin' name='resourceId[]' value='{$resource['resourceId']}' checked='checked'><label>{$resource['resourceName']}</label></li>";
                                        else
                                            echo "<li><input type='checkbox' class='resourceIdAdmin' name='resourceId[]' value='{$resource['resourceId']}'><label>{$resource['resourceName']}</label></li>";
                                    }
                                }
                            ?>
                            </ul>
                                <div class="clear-both"></div>
                            </div>
                        </div>
                    </div>
                </dd>
                <div class="clear-both"></div>
                <dt></dt>
                <dd id="submit-element">
                    <input value="Lưu thay đổi" class="button" type="submit" />
                    <input value="Reset" class="button" type="reset"/>
                </dd>
            </dl>
        </form>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('.partnerId').change (function (){
            var id = $(this).val();
            if ( this.checked ) {
            $.get("http://local.admin.partner.vn/admin/game/getgame/partnerId/"+id, function(result) { 
                   result = $.parseJSON(result);
                   $('#game').append('<div id="game'+id+'"></div>');
                    $('#game'+id).append(' <div class="clear-both"></div>   '
                        +'<dd><div class="option-wrapper_partner" style="margin-left:120px;"><div class="fieldset" >   '
                        +'<div class="legend_partner"><label >Game của '+id+'</label></div>'
                        +'<div class="fieldset-content"><ul id=game_'+id+'>'
                        +'</ul><div class="clear-both"></div></div></div> </div> </dd>'
                        );       
                   $.each(result, function(i, v){
                       var check='<li><input type="checkbox" class="gameId" name="gameId_'+id+'[]" value='+v.gameId+'> <label id="gameId">'+v.gameName+'</label></li>';
                         $('#game_'+id).append($(check)) ;
                   });
            })
            }
            if(!this.checked){
                $('#game'+id).empty();
            }
        });
        
    });
     
</script>